//
//  JkmRefreshControl.swift
//  SinaWb
//
//  Created by Jiang on 6/2/16.
//  Copyright © 2016 Jiang. All rights reserved.
//

import UIKit
import SnapKit
class JkmRefreshControl: UIRefreshControl {
    var hasRotato = false
    lazy var refreshView : RefreshView = {
        let view = RefreshView()
        return view
    }()
    lazy var loadview : loadView = {
        let view = loadView()
        return view
    }()
    override init() {
        super.init()
        addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions.New, context: nil)
        addSubview(loadview)
        addSubview(refreshView)
        
        refreshView.snp_makeConstraints { (make) in
            make.left.equalTo(snp_left)
            make.top.equalTo(snp_top)
            make.width.equalTo(UIScreen.mainScreen().bounds.width)
            make.height.equalTo(44)
        }
        loadview.snp_makeConstraints { (make) in
            make.edges.equalTo(refreshView.snp_edges)
        }
    }
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if frame.origin.y == 0 || frame.origin.y > 0 {
            return
        }
        if refreshing {
            refreshView.hidden = true
            loadview.startAnimation()
        }
        if frame.origin.y > -60 && hasRotato{
            
            hasRotato = false
            refreshView.imageViewReturnBefore()
            
        }else if (!hasRotato && frame.origin.y < -60){
            
            refreshView.imageViewMakeRotate()
            hasRotato = true
            
        }
    }
    override func beginRefreshing() {
        super.beginRefreshing()
        
    }
    override func  endRefreshing() {
        super.endRefreshing()
        refreshView.hidden = false
        loadview.imageView.layer.removeAllAnimations()
        
    }
    deinit{
        removeObserver(self, forKeyPath: "frame")
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}


class RefreshView: UIView {
    lazy var imageView : UIImageView = {
        let imageview = UIImageView(image: UIImage(named: "tableview_pull_refresh"))
        return imageview
    }()
    lazy var textLable : UILabel = {
       let lable = UILabel()
        lable.text = "下拉刷新"
        lable.font = UIFont.systemFontOfSize(14)
        lable.textColor = UIColor.lightGrayColor()
        lable.textAlignment = .Left
        return lable
    }()
    init(){
        super.init(frame: CGRectZero)
        Setting()
            
    }
    
    func Setting(){
        addSubview(imageView)
        addSubview(textLable)
        backgroundColor = UIColor.whiteColor()
        imageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(snp_centerY)
            make.centerX.equalTo(snp_centerX).offset(-30)
        }
        textLable.snp_makeConstraints { (make) in
            make.left.equalTo(imageView.snp_right)
            make.centerY.equalTo(imageView.snp_centerY)
        }
    
    }
    
    func imageViewMakeRotate(){
        UIView.animateWithDuration(0.2) {
            self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, CGFloat(-M_PI_2))
            self.imageView.transform =  CGAffineTransformRotate(self.imageView.transform, CGFloat(-M_PI_2))
        }
        self.textLable.text = "释放刷新"
    
    }
    func imageViewReturnBefore(){
        UIView.animateWithDuration(0.2) {
            self.imageView.transform = CGAffineTransformIdentity
        }
        self.textLable.text = "下拉刷新"
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class loadView : RefreshView{
    override init() {
        super.init()
        imageView.image = UIImage(named: "tableview_loading")
        textLable.text = "加载刷新"
    }
    
    func  startAnimation(){
        
        let ani =  imageView.layer.animationForKey("animation")
        guard ani == nil else{
            return
        }
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.repeatCount = MAXFLOAT
        animation.duration = 1
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion =  false
        
        imageView.layer.addAnimation(animation, forKey: "animation")
        
    
    
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



}